const md = require('../../utils/md5.js');
const app = getApp();
const post = require('../../utils/post.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    alllist: [],
    lastlist: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  //搜索供应链
  search: function () {

    var that = this;
    // console.log(clears)
    var key = that.data.key;
    if (!key) {
      that.chushihua();
      return;
    }
    var datas = {};
    datas.where = "MyMebID = '" + wx.getStorageSync('userinfo').MebID + "' and (FrMebName like '%" + key + "%' or FrDepName like '%" + key + "%')";
    console.log(datas.where);
    var datacall = function (res) {
      console.log(res);
      if (res.data.data.length > 0) {
        var data = res.data.data;
        for (var i = 0; i < data.length; i++) {
          data[i].isTouchMove = 1;
          console.log(data[i].FrDepName.length);
          if (data[i].FrDepName) {
            if (data[i].FrDepName.length > 8) {
              data[i].FrDepName = data[i].FrDepName.substring(0, 8) + "...";
            }
          }
          if (data[i].FrMebName) {
            if (data[i].FrMebName.length > 5) {
              data[i].FrMebName = data[i].FrMebName.substring(0, 5) + "...";
            }
          }else{
            data[i].FrMebName = data[i].FrNickName;
            if (data[i].FrMebName.length > 5) {
              data[i].FrMebName = data[i].FrMebName.substring(0, 5) + "...";
            }
          }
          if (data[i].FrBrands) {
            if (data[i].FrBrands.length > 12) {
              data[i].FrBrands = data[i].FrBrands.substring(0, 12) + "...";
            }
          }
          if (data[i].FrServiceArea) {
            if (data[i].FrServiceArea.length > 6) {
              data[i].FrServiceArea = data[i].FrServiceArea.substring(0, 6) + "..";
            }
          }

        }
        that.setData({
          alllist: data
        })
      } else {
        that.setData({
          alllist: []
        })
      }
    }
    var res2 = post.getdata("/BO/BA_Friends/Query", datas, datacall);
  },
  getkey: function (e) {
    this.setData({
      key: e.detail.value
    })
  },
  onShow: function () {
    this.chushihua();
  },
  chushihua: function () {
    var that = this;
    var apinameIsMeb = "/Action/PK01/GetFriends";
    var timestamp = new Date().getTime();
    timestamp = parseInt(timestamp / 1000);
    var md5IsMeb = md.hexMD5(app.server.appid + timestamp + apinameIsMeb + app.server.appkey);
    wx.request({
      //后台接口地址
      url: app.server.url + apinameIsMeb,
      data: {
        sign: md5IsMeb,
        appid: app.server.appid,
        tmsp: timestamp,
        MebID: wx.getStorageSync('userinfo').MebID
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: function (res) {
        console.log(res);
        if (res.data.code == 0) {
          var datas = res.data.data.All;
          for (var i = 0; i < datas.length; i++) {
            datas[i].isTouchMove = 1;
            if (datas[i].FrDepName) {
              if (datas[i].FrDepName.length > 8) {
                datas[i].FrDepName = datas[i].FrDepName.substring(0, 8) + "...";
              }
            }
            if (datas[i].FrMebName) {
              if (datas[i].FrMebName.length > 8) {
                datas[i].FrMebName = datas[i].FrMebName.substring(0, 8) + "...";
              }
            }else{
              data[i].FrMebName = data[i].FrNickName;
              if (data[i].FrMebName.length > 5) {
                data[i].FrMebName = data[i].FrMebName.substring(0, 5) + "...";
              }
            }
            if (datas[i].FrBrands) {
              if (datas[i].FrBrands.length > 12) {
                datas[i].FrBrands = datas[i].FrBrands.substring(0, 12) + "...";
              }
            }
            if (datas[i].FrServiceArea) {
              if (datas[i].FrServiceArea.length > 6) {
                datas[i].FrServiceArea = datas[i].FrServiceArea.substring(0, 6) + "..";
              }
            }
          }
          that.setData({
            alllist: datas,
            lastlist: res.data.data.Last
          })
        }
      }
    })
  },
  tofind: function () {
    var status = wx.getStorageSync('newinfo').Status;
    var isservice = wx.getStorageSync('userinfo').IsService;
    if(status && status=='S'){
      wx.navigateTo({
        url: '/pages/find/find',
      })
    }else{
      if(isservice && isservice!='null'){
        wx.navigateTo({
          url: '/pages/find/find',
        })
        return;
      }
      wx.showModal({
        title: '',
        content: '当前状态没有权限使用发现功能',
      })
    }
    
  },
  //手指触摸动作开始 记录起点X坐标
  touchstart: function (e) {
    //开始触摸时 重置所有删除
    this.data.alllist.forEach(function (v, i) {
      console.log(v.isTouchMove);
      if (v.isTouchMove)//只操作为true的
        v.isTouchMove = 1;
    })
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
      alllist: this.data.alllist
    })
  },

  //滑动事件处理
  touchmove: function (e) {
    var that = this,
      index = e.currentTarget.dataset.index,//当前索引
      startX = that.data.startX,//开始X坐标
      startY = that.data.startY,//开始Y坐标
      touchMoveX = e.changedTouches[0].clientX,//滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY,//滑动变化坐标
      //获取滑动角度
      angle = that.angle({ X: startX, Y: startY }, { X: touchMoveX, Y: touchMoveY });
    that.data.alllist.forEach(function (v, i) {
      v.isTouchMove = 1
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        if (touchMoveX > startX) //右滑
        {
          v.isTouchMove = 1
        }
        else //左滑
        {
          v.isTouchMove = 2
        }
      }
    })
    //更新数据
    that.setData({
      alllist: that.data.alllist
    })
  },
  /**
* 计算滑动角度
* @param {Object} start 起点坐标
* @param {Object} end 终点坐标
*/
  angle: function (start, end) {

    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  tolinkman: function (e) {
    wx.navigateTo({
      url: '/pages/linkman/linkman?id=' + e.currentTarget.dataset.id,
    })
  },
  del: function (e) {
    var that = this
    var index = e.target.dataset.index;
    // console.log(that.data.alllist[index].isTouchMove)
    // if (that.data.alllist[index].isTouchMove == 1){
    //   return;
    // }

    wx.showModal({
      title: '提示',
      content: '是否确定删除该联系人！！！',
      success: function (res) {
        if (res.confirm) {
          //获取列表中要删除项的下标   
          var id = e.target.dataset.id;
          console.log(id)
          var alllist = that.data.alllist;
          //移除列表中下标为index的项  
          alllist.splice(index, 1);
          //更新列表的状态  
          that.setData({
            alllist: alllist
          });
          //删除联系人
          var datas = {};
          datas.RelID = id;
          var datacall = function (res) {
            console.log(res);
          }
          var res2 = post.getdata("/BO/BA_Friends/Delete", datas, datacall);

        } else {
          // initdata(that)
        }
      }
    })
  },
  clearall: function () {
    var that = this;
    that.setData({
      key: ''
    });
    that.search()
  }
})